package database

import (
	"context"
	"fmt"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"log"
)
var client *mongo.Client
var DB *mongo.Database

func InitDB() (client *mongo.Client) {
	//设置客户端连接配置
	//clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
	clientOptions := options.Client().ApplyURI("mongodb://admin:sdl%40admin@82.156.109.119:27017")	//测试
	//clientOptions := options.Client().ApplyURI("mongodb://admin:sdl%40admin@106.52.170.16:27017")	//正式
	//连接到吗mongodb
	client, err := mongo.Connect(context.TODO(), clientOptions)

	if err != nil {
		log.Println(err)
	}
	fmt.Println("Connected to MongoDB!")
	return client
}
//返回数据库类型指针

func GetMongoDB() *mongo.Database {
	if client == nil {
		client = InitDB()
	}
	DB = client.Database("graphite")
	return  DB
}

